package com.appstore.controller;

import com.appstore.common.AjaxResult;
import com.appstore.domain.entity.AppComment;
import com.appstore.service.AppCommentService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/app_comment")
public class AppCommentController {

    @Resource
    private AppCommentService appCommentService;

    @PostMapping
    public AjaxResult addAppComment(@RequestBody AppComment appComment) {
        if(ObjectUtils.isEmpty(appComment)) return AjaxResult.error("请求参数为空");
        int insert = appCommentService.addAppComment(appComment);
        if(insert > 0){
            return AjaxResult.success("新增成功",insert);
        }else {
            return AjaxResult.error("新增失败");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteAppComment(@PathVariable Long id) {
        if(ObjectUtils.isEmpty(id)) return AjaxResult.error("请求参数为空");
        int delete = appCommentService.deleteAppComment(id);
        if(delete > 0){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败");
        }
    }

    @GetMapping("/{id}")
    public AjaxResult getAppComment(@PathVariable Long id) {
        if(ObjectUtils.isEmpty(id)) return AjaxResult.error("请求参数为空");
        return AjaxResult.success(appCommentService.getAppComment(id));
    }

    @GetMapping("/getAppCommentListByAppId/{appId}")
    public AjaxResult getAppCommentListByAppId(@PathVariable Long appId) {
        if(ObjectUtils.isEmpty(appId)) return AjaxResult.error("请求参数为空");
        return AjaxResult.success(appCommentService.getAppCommentListByAppId(appId));
    }
}
